#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

create_infra $namespace
deploy_cert_manager

desc 'create first PXC cluster'
cluster="some-name"
spinup_pxc "$cluster" "$conf_dir/$cluster.yml" "3" "10" "${conf_dir}/secrets_without_tls.yml"

desc 'check if cert-manager issued ssl certificates'
if [ "$(kubectl get secrets ${cluster}-ssl -o jsonpath='{.metadata.annotations.cert-manager\.io/issuer-kind}')" != "Issuer" ]; then
  echo "Cert manager didn't create the ssl certificates! Something went wrong."
  exit 1
fi

desc 'check if service and statefulset created with expected config'
compare_kubectl statefulset/$cluster-pxc
compare_kubectl statefulset/$cluster-proxysql
compare_kubectl service/$cluster-pxc
compare_kubectl service/$cluster-proxysql
compare_kubectl service/$cluster-proxysql-unready

desc 'check PodDisruptionBudget'
compare_kubectl pdb/$cluster-pxc
compare_kubectl pdb/$cluster-proxysql

desc 'check if MySQL users created'
compare_mysql_user "-h $cluster-pxc -uroot -proot_password"
compare_mysql_user "-h $cluster-pxc -umonitor -pmonitor"
compare_mysql_user "-h $cluster-pxc -uproxyuser -ps3cret"
compare_mysql_user_local "-uxtrabackup -pbackup_password" "$cluster-pxc-0" "" -c' pxc'
compare_mysql_user_local "-uclustercheck -pclustercheckpassword" "$cluster-pxc-0" "" -c'pxc'
# check that pmm server user don't have access
compare_mysql_user "-h $cluster-pxc -upmmserver -pmonitor"

desc 'check if ProxySQL users created'
compare_mysql_user "-h $cluster-proxysql -uroot -proot_password"
compare_mysql_user "-h $cluster-proxysql -umonitor -pmonitor"
# check that pmm server user don't have access
compare_mysql_user "-h $cluster-proxysql -uproxyuser -ps3cret" "-proxysql"
compare_mysql_user "-h $cluster-proxysql -upmmserver -pmonitor" "-proxysql"

desc 'write data directly, read from all'
run_mysql \
    'INSERT myApp.myApp (id) VALUES (100501)' \
    "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-2" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"

desc 'kill Primary Pod, check reelection, check data'
initial_primary=$(get_proxy_primary "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0")
kubectl_bin delete pods --grace-period=0 --force $initial_primary
sleep 10
changed_primary=$(get_proxy_primary "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0")
[ -n "$changed_primary" ] && [ "$initial_primary" != "$changed_primary" ]
run_mysql \
    'INSERT myApp.myApp (id) VALUES (100502)' \
    "-h $cluster-proxysql -uroot -proot_password"
wait_for_running "$cluster-pxc" 3
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
compare_mysql_cmd "select-3" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"

compare_mysql_cmd "max_allowed_packet-1" "SELECT @@max_allowed_packet;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"

desc 'check if possible to create no-proxysql cluster'
cluster3="no-proxysql"
apply_config "$test_dir/conf/$cluster3.yml"
wait_for_running "$cluster3-pxc" 3
desc 'check if service and statefulset created'
compare_kubectl service/$cluster3-pxc
compare_kubectl service/$cluster3-proxysql
compare_kubectl configmap/$cluster3-pxc
desc 'check if storageClass changed'
compare_kubectl statefulset/$cluster3-pxc
compare_kubectl statefulset/$cluster3-proxysql
compare_kubectl issuer/$cluster3-pxc-ca
compare_kubectl certificate/$cluster3-ssl
compare_kubectl certificate/$cluster3-ssl-internal

desc 'write data, read from all'
run_mysql \
    'CREATE DATABASE IF NOT EXISTS myApp; use myApp; CREATE TABLE IF NOT EXISTS myApp (id int PRIMARY KEY);' \
    "-h $cluster3-pxc -uroot -proot_password"
run_mysql \
    'INSERT myApp.myApp (id) VALUES (100500)' \
    "-h $cluster3-pxc -uroot -proot_password"
sleep 10
compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster3-pxc-0.$cluster3-pxc -uroot -proot_password"
compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster3-pxc-1.$cluster3-pxc -uroot -proot_password"
compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster3-pxc-2.$cluster3-pxc -uroot -proot_password"
compare_mysql_cmd "max_allowed_packet-2" "SELECT @@max_allowed_packet;" "-h $cluster3-pxc-0.$cluster3-pxc -uroot -proot_password"
kubectl_bin delete -f $test_dir/conf/$cluster3.yml

destroy $namespace
